/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.battle;

import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;

/**
 *
 * @author Jakub Kutil
 */
public class ThresholdValidator implements IValidator<String>{

    @Override
    public void validate(IValidatable<String> iv) {
        final String field = iv.getValue();
        
        if(field.isEmpty()){
              error(iv, "notNumber");
            
        }
        try{
        int number = Integer.parseInt(field);
        if(number < 20){
             error(iv, "lesserThan20");
        }
         if(number > 100){
             error(iv, "greaterThan100");
        }
        }
        catch(NumberFormatException e){
            error(iv, "notNumber");           
            }
        }
    
    private void error(IValidatable<String> validatable, String errorKey) {
		ValidationError error = new ValidationError();
		error.addMessageKey(getClass().getSimpleName() + "." + errorKey);
		validatable.error(error);
	}
    }